package com.sgb.kjwl.viewmodel.shopping;


import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.sgb.jicai.entity.FactoryCentralizedListEntity;
import com.sgb.jicai.entity.JiCaiHomeListRequestBean;
import com.sgb.jicai.entity.FactoryCentralizedItemEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.ShoppingCarRepertory;
import com.sgb.kjwl.view.ui.fragment.shop.ShopJcChildFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

public class ShopJcChildModel extends BaseViewModel {
    private ShopJcChildFragment fragment;
    private ShoppingCarRepertory repertory;
    private int page = 1;
    private String url;

    public ShopJcChildModel(ShopJcChildFragment fragment, ShoppingCarRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    //需方发起和企业专项数据
    public void getJiCaiList(JiCaiHomeListRequestBean map, boolean loadMore) {
        page = loadMore ? page + 1 : 1;
        map.setCurrent(page);
        map.setLimit(8);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCentralizedMiningAPI().getJiCaiLists(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<CentralizedMiningEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (!loadMore) {
                        fragment.showList(data.getData().getList(), data.getData().isLastPage());
                    } else {
                        fragment.lodMore(data.getData().getList(), data.getData().isLastPage());
                    }
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    //供方发起数据
    public void getGongFangList(JiCaiHomeListRequestBean listBean, boolean loadMore) {
        page = loadMore ? page + 1 : 1;
        listBean.setCurrent(page);
        listBean.setLimit(8);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCentralizedMiningAPI().getFactoryCentralizedLists(listBean).enqueue(new ICallBack<BaseEntity<FactoryCentralizedListEntity<FactoryCentralizedItemEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<FactoryCentralizedListEntity<FactoryCentralizedItemEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null) {
                    return;
                }
                boolean isLastPage = !(data.getData().getTotal() > data.getData().getCurrent());//判断是否是最后一页可能有误
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (!loadMore) {
                        fragment.showGongFangList(data.getData().getList(), isLastPage);
                    } else {
                        fragment.lodGongFangMore(data.getData().getList(), isLastPage);
                    }
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    //跳转到H5页面
    public void getH5Url(int type, String id) {
        String name = "";
        if (type == 1) {
            name = "collection";
        } else {
            name = "Factory";
        }
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(name).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {//type 0 首页 1 我的集采 2 我的报价
                    SharedPreferenceUtil.init(fragment.getActivity());
                    if (type == 1) {
                        url = data.getData().getUrl() + "?id=" + id + "&type=0" + "&token=" + repertory.getToken();
                    } else {
                        url = data.getData().getUrl() + "?id=" + id;
                    }
                    if (!TextUtils.isEmpty(url)) {
                        fragment.showUrl(url);
                    }

                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }
}
